package org.iwidget.desktop.core.bsf;
import org.iwidget.desktop.core.*;
import org.iwidget.desktop.model.WidgetElement;
import java.io.File;
import java.util.StringTokenizer;
/**
 *
 * @author Muhammad Hakim A
 */


public class IwidgetSystemBSF
{

    public IwidgetSystemBSF(WidgetElement widget)
    {
        this.widget = widget;
    }

    public String getOSName()
    {
        return System.getProperty("os.name");
    }

    public String getOSVersion()
    {
        return System.getProperty("os.version");
    }

    public String getOSArchitecture()
    {
        return System.getProperty("os.arch");
    }

    public String getPathSeparator()
    {
        return System.getProperty("path.separator");
    }

    public String getFileSeparator()
    {
        return System.getProperty("file.separator");
    }

    public String getUserName()
    {
        return System.getProperty("user.name");
    }

    public String getUserHomeFolder()
    {
        return System.getProperty("user.home");
    }

    public String getUserCurrentFolder()
    {
        return System.getProperty("user.dir");
    }

    public String[] getRootFolders()
    {
        File roots[] = File.listRoots();
        String rootFiles[] = new String[roots.length];
        for(int iCtr = 0; iCtr < roots.length; iCtr++)
            rootFiles[iCtr] = roots[iCtr].getAbsolutePath();

        return rootFiles;
    }

    public boolean isFile(String pathName)
    {
        File file = new File(pathName);
        return file.isFile();
    }

    public boolean canReadFile(String pathName)
    {
        File file = new File(pathName);
        return file.canRead();
    }

    public boolean canWriteFile(String pathName)
    {
        File file = new File(pathName);
        return file.canWrite();
    }

    public String stringReplace(String s, String s1, String s2)
    {
        String output = Iwidget.replace(s, s1, s2, 0);
        return output;
    }

    public String getRunResult()
    {
        return widget.getIwidgetData().runResult;
    }

    public char getKey()
    {
        return widget.getIwidgetData().key;
    }

    public String getKeyString()
    {
        return widget.getIwidgetData().keyString;
    }

    public String getKeyModifiers()
    {
        return widget.getIwidgetData().keyModifiers;
    }

    public String getDroppedObjectType()
    {
        return widget.getIwidgetData().droppedObjectsType;
    }

    public String[] getDroppedObjects()
    {
        return widget.getIwidgetData().droppedObjects;
    }

    public long diskFree(String path)
    {
        try{
            if(System.getProperty("os.name").startsWith("Windows"))
            {
                if(path == null || path.length() != 2 || path.charAt(1) != ':')
                    path = "c:";
                
                String dir = IwidgetMethods.runInternal("dir /-C " + path);
                dir = dir.substring(0, dir.length() - 1).trim();
                int index = dir.lastIndexOf('\n');
                dir = dir.substring(index, dir.length() - 11);
                index = dir.lastIndexOf(' ');
                dir = dir.substring(index + 1, dir.length());
                
                if(dir == null || dir.length() == 0)
                    return 0L;
                else
                    return Long.parseLong(dir);
            }
            if(path == null)
                path = "/";
            
            StringTokenizer tokens;
            String dir = IwidgetMethods.runInternal("df " + path);
            int index = dir.indexOf('\r');
            dir = dir.substring(index + 2, dir.length());
            tokens = new StringTokenizer(" \t");
            tokens.nextElement();
            tokens.nextElement();
            tokens.nextElement();
            return Long.parseLong((String)tokens.nextElement());
        }catch(Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    private WidgetElement widget;
}